﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class D_AuctionMarket
    {
        public bool CreateOneAuctionMarket(AuctionMarket_Info objAuctionMarket_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("AuctionMarket_Insert", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"AuctionMarketID", objAuctionMarket_Info.AuctionMarketID));
            command.Parameters.Add(new SqlParameter(@"AuctionCode", objAuctionMarket_Info.AuctionCode));
            command.Parameters.Add(new SqlParameter(@"AuctionMarketName", objAuctionMarket_Info.AuctionMarketName));
            command.Parameters.Add(new SqlParameter(@"AuctionMarketDate", objAuctionMarket_Info.AuctionMarketDate));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //**********************************************************************************       
        public bool EditOneAuctionMarket(AuctionMarket_Info objAuctionMarket_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("AuctionMarket_Update", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@AuctionMarketID", objAuctionMarket_Info.AuctionMarketID));
            command.Parameters.Add(new SqlParameter("@AuctionCode", objAuctionMarket_Info.AuctionCode));
            command.Parameters.Add(new SqlParameter("@AuctionMarketName", objAuctionMarket_Info.AuctionMarketName));
            command.Parameters.Add(new SqlParameter("@AuctionMarketDate", objAuctionMarket_Info.AuctionMarketDate));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public bool RemoveOneAuctionMarket(int AuctionMarketID)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("AuctionMarket_Delete", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"AuctionMarketID", AuctionMarketID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public DataTable GetAllAuctionMarket()
        {
            DataSet ds = new DataSet();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("AuctionMarket_SelectAll", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            try
            {
                objConnection.Open();
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(ds);
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return ds.Tables[0];
        }
        //******************************************************************************       
        public AuctionMarket_Info GetOneAuctionMarket(int AuctionMarketID)
        {
            AuctionMarket_Info objAuctionMarket_Info = new AuctionMarket_Info();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("AuctionMarket_SelectByID", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"AuctionMarketID", AuctionMarketID));
            try
            {
                objConnection.Open();
                SqlDataReader ordAuctionMarket = command.ExecuteReader(); //OjectRecordDatareader
                ordAuctionMarket.Read();
                if (ordAuctionMarket.HasRows)
                {
                    if (ordAuctionMarket["AuctionMarketID"] != DBNull.Value)
                    {
                        objAuctionMarket_Info.AuctionMarketID = Convert.ToInt16(ordAuctionMarket["AuctionMarketID"]);
                    }
                    if (ordAuctionMarket["AuctionCode"] != DBNull.Value)
                    {
                        objAuctionMarket_Info.AuctionCode = Convert.ToString(ordAuctionMarket["AuctionCode"]);
                    }
                    if (ordAuctionMarket["AuctionMarketName"] != DBNull.Value)
                    {
                        objAuctionMarket_Info.AuctionMarketName = Convert.ToString(ordAuctionMarket["AuctionMarketName"]);
                    }
                    if (ordAuctionMarket["AuctionMarketDate"] != DBNull.Value)
                    {
                        objAuctionMarket_Info.AuctionMarketDate = Convert.ToDateTime(ordAuctionMarket["AuctionMarketDate"]);
                    }
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return objAuctionMarket_Info;
        }
    }
}
